package com.landiao.ctrler.management;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.landiao.common.Constant;
import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.ResultMsg;
import com.landiao.common.utils.CodecUtils;
import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.BackstageUser;
import com.landiao.service.BackstageUserService;

@Controller
@RequestMapping("/management/backstageuser")
public class BackstageUserCtrler extends BaseCRUDCtrler<BackstageUser, String> {

	private BackstageUserService getBackstageUserService() {
        return (BackstageUserService) baseService;
    }
	
	@RequestMapping("index")
	public String index(Model model) {
		if(this.getClientUser().getUserType()==Constant.USER_TYPE_MAIN)
			return viewName("index");
		else
			return "management/index";
	}
	
	@Override
	public String save(BackstageUser t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		try{
			t.setPasswd(CodecUtils.password("123456"));
			t.setBackstageUserId(CodecUtils.getUUID());
			t.setCompId(this.getClientUser().getUser().getCompId());
			baseService.save(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("添加成功,初始密码为：123456"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("添加失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("添加失败"));
		}
		return redirectToUrl(viewName("index"));
	}

	
	@Override
	public String update(BackstageUser t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		List<BackstageUser> list = this.getBackstageUserService().getMainAccByCompId(this.getClientUser().getUser().getCompId());
		if(list==null||list.size()==0||
				(list.size()==1&&list.get(0).getBackstageUserId().equals(t.getBackstageUserId())&&!list.get(0).getShopId().equals(t.getShopId()))){
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改失败，必须有一个总店管理员！！"));
			return redirectToUrl(viewName("index"));
		}
		return super.update(t, redirectAttr);
	}

	/**
	 * 验证账号是否存在
	 * 
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String account,String id) {
		BackstageUser user = this.getBackstageUserService().getByAccount(account);
		if (user == null) {
			return true;
		}else{
			if(user.getBackstageUserId().equals(id)){
				return true;
			}else{
				return false;
			}
		}
	}
	
	/**
	 * 重置密码
	 * 
	 */
	@RequestMapping(value = "resetPasswd/{id}", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg resetPasswd(@PathVariable String id) {
		BackstageUser user = this.getBackstageUserService().getById(id);
		user.setPasswd(CodecUtils.password("123456"));
		this.getBackstageUserService().update(user);
		return ResultMsgFactory.success("重置密码成功，密码为：123456");
	}
	
	@RequestMapping(value = "updatePasswordUi")
	public String updatePasswordUi(Model model) {
		BackstageUser user = this.getClientUser().getUser();
		model.addAttribute("t", user);
		return viewName("updatePassword");
	}
	
	@RequestMapping(value = "updatePassword")
	public String updatePassword(String oldPassword,String newPassword,RedirectAttributes redirectAttr) {
		try{
			BackstageUser user = this.getClientUser().getUser();
			String oldPwd = CodecUtils.password(oldPassword);
			if(!user.getPasswd().equals(oldPwd)){
				redirectAttr.addFlashAttribute("resultMsg",
						ResultMsgFactory.success("原密码不正确"));
			}else{
				user.setPasswd(CodecUtils.password(newPassword));
				this.getBackstageUserService().update(user);
				redirectAttr.addFlashAttribute("resultMsg",
						ResultMsgFactory.success("修改成功"));
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error("修改失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl(viewName("index"));
	}
}
